/*  BlogBotPanel.java Version 102 - 27th May 2008
 *  BlogBotPanel.java is responsible for handling the GUI
 *  See Versions for program version number
 *  Copyright (C) 2008  Andy Broomfield andy@newmediathinking.com
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

package net.blogbotplatform.blogbot;

import java.awt.*;
import java.awt.event.*;

/*this is the main GUI. It is a complete mess and needs to be thought out
 * properley. Eventully, this should become the staring point for 
 * confiquring and running different kinds of blog bots, 
 * that is for the future
*/
public class BlogBotPanel extends Panel implements ActionListener, ItemListener {
	private static final long serialVersionUID = -1612963358673849699L;

	//Gui elements Required
	Button bStart;
	Button bStop;
	Choice serialPortChoice;	
	Choice parallelPortChoice;
	TextArea outputTA;
	
	boolean runBlogBot; // are we running?
	
	int availableSerialPorts = 0; // cheeky way of checking if the ports change
	int availableParallelPorts = 0; // cheeky way of checking if the ports change

	TimestepTask timestepTask;
	
	BlogBotService bbsv; // get a new service
	ProbePorts pp; // get a new port prober
	
	String lastBlogText = BlogBotShop.screenBlogText;

	public BlogBotPanel() {
		// first establish the other serivices that are spawned by the GUI
		//establish a new Blog Bot Service
		bbsv = new BlogBotService(this);
		
		//establish the port prober
		pp = new ProbePorts();
		pp.listPorts();
		
		//lays out the main GUI
		this.setLayout(new BorderLayout(1, 1));
		//setBackground(Color.lightGray);

		Panel toolsPanel = new Panel();
		toolsPanel.setLayout(new GridLayout(4,2));
		this.add(toolsPanel, BorderLayout.NORTH);

		bStart = new Button("Start Blog Bot");
		bStart.addActionListener(this);
		toolsPanel.add(bStart);

		bStop = new Button("Stop Blog Bot");
		bStop.addActionListener(this);
		toolsPanel.add(bStop);
				
		//choice menu - serial port
		toolsPanel.add(new Label("Serial Port"));
		
		serialPortChoice = new Choice();
		buildSerialPortChoice();
		serialPortChoice.addItemListener(this);
		toolsPanel.add(serialPortChoice);
		
		//choice menu - parallel port
		toolsPanel.add(new Label("Parallel Port"));
		
		parallelPortChoice = new Choice();
		buildParallelPortChoice();
		parallelPortChoice.addItemListener(this);
		toolsPanel.add(parallelPortChoice);
		
		outputTA = new TextArea(20, 60);
		this.add(outputTA, BorderLayout.CENTER);
		
		runBlogBot = false;

		//this.pack();
		//this.setVisible(true);

		timestepTask = new TimestepTask(this);
		timestepTask.start();
	}

	void buildSerialPortChoice() {
		System.out.println("Building Serial Port Choice");
		serialPortChoice.removeAll();
		serialPortChoice.add("Off");
		serialPortChoice.add("Simulator");
		for (int i=0;i<pp.avalibleSerialPorts.size();i++) {
			serialPortChoice.add((String)pp.avalibleSerialPorts.get(i));
		}
	}
	
	void buildParallelPortChoice() {
		System.out.println("Building Parallel Port Choice");
		parallelPortChoice.removeAll();
		for (int i=0;i<pp.avalibleParallelPorts.size();i++) {
			parallelPortChoice.add((String)pp.avalibleParallelPorts.get(i));
		}
	}
	
	synchronized void advance() {
		//System.out.println("advanceing...");
		//pp.listPorts();
		if(availableSerialPorts != pp.avalibleSerialPorts.size()) {
			availableSerialPorts = pp.avalibleSerialPorts.size();
			buildSerialPortChoice();
		}
		if(availableParallelPorts != pp.avalibleParallelPorts.size()) {
			availableParallelPorts = pp.avalibleParallelPorts.size();
			buildParallelPortChoice();
		}
		
		//update the blog screen text
		/*if (BlogBotShop.screenBlogText != lastBlogText) {
			outputTA.append(BlogBotShop.screenBlogText);
			lastBlogText = BlogBotShop.screenBlogText;
		}*/
		
		if (runBlogBot == true) {
			bbsv.runBlogBotService();
		}

	}

	public void actionPerformed(ActionEvent event) {
		Object source = event.getSource();

		if(source == bStart) {
			//pressing start, wake the service
			System.out.println("Pressed Start!");
			if (runBlogBot == false) {
				runBlogBot = true;
				bbsv.wakeService();
			}
		}

		if(source == bStop) {
			//pressing stop, sleep the service
			System.out.println("Pressed Stop!");
			if (runBlogBot == true) {
				runBlogBot = false;
				bbsv.sleepService();
			}
		}
	}
	
	public void itemStateChanged(ItemEvent event) {
		Object source = event.getSource();
		// TODO Auto-generated method stub
		//System.out.println(source);
		
		if(source == serialPortChoice) {
			//System.out.println("hurray");
			System.out.println(serialPortChoice.getSelectedItem());
			bbsv.changeSerialPort(serialPortChoice.getSelectedItem());
		}
		
		if(source == parallelPortChoice) {
			//System.out.println("hurray");
			System.out.println(parallelPortChoice.getSelectedItem());
			bbsv.changeParallelPort(parallelPortChoice.getSelectedItem());
		}
	}
	
	public void stopTask() {
		//stopping the panel (quit handlers)
		if (runBlogBot == true) {
			runBlogBot = false;
			bbsv.stopService();
		}
		timestepTask.stopTask();
		//bbpa.stop();
	}
}

class TimestepTask extends Thread {
	//this runs the actual service.
	BlogBotPanel blogBotPanel;

	boolean running = true;

	TimestepTask(BlogBotPanel blogBotPanel) {
		this.blogBotPanel = blogBotPanel;
	}

	void stopTask() {
		running = false;
	}

	public void run() {
		while (running) {
			blogBotPanel.advance();
			try { Thread.sleep(10); } catch (Exception e) {}
		}
	}
}
